home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / hypercrd / hc2_x / tcprogud.sit / TC Prog Guide / card_2542.txt < prev    next >
Text File  |  1991-02-27  |  2KB  |  34 lines

  1. -- card: 2542 from stack: in
  2. -- bmap block id: 0
  3. -- flags: 0000
  4. -- background id: 4755
  5. -- name: 
  6.  
  7.  
  8. -- part contents for background part 7
  9. ----- text -----
  10. 39
  11.  
  12. -- part contents for background part 6
  13. ----- text -----
  14. 1.2  Inheritance & Polymorphism
  15.  
  16. -- part contents for background part 4
  17. ----- text -----
  18. At this point, the reader may wonder why we haven't provided an instance variable of Person which is a character string, to contain a person's name.  As mentioned earlier, character strings are represented in C as arrays of char variables.  For simplicity, we will postpone discussion of how C handles arrays*.  However, it should be noted that if the programmer later decides to implement such a feature in the person.c program, no change will be required in the main() function.  This is one of the benefits of modular program design, where the functions using an object are "shielded" from the implementation of the object and any changes thereto.
  19.  
  20. Of even greater significance is the ability to derive classes from existing classes in TC and C++.  Derived classes INHERIT the instance variables and methods of the "base" class, and may add new instance variables and methods.  New methods may be defined with the same names as those in the base class, in which case they "override" the original methods.  This property of POLYMORPHISM means that the same messages can be sent to objects of different classes, with results appropriate to each.
  21.  
  22.  The following program listing illustrates these concepts by deriving the Student class from the Person class of the previous example.
  23.  
  24. -- part contents for background part 27
  25. ----- text -----
  26. Arrays, pointers, and strings
  27.  
  28. -- part contents for background part 20
  29. ----- text -----
  30. Arrays, pointers, and strings - p59
  31.  
  32. -- part contents for background part 29
  33. ----- text -----
  34. 22415